package com.ebs.md.fxmonitor.marketmonitor.adapter;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/adapter/ParsedPayloadToObjAdapter.java $
 *  $Revision: #3 $  $Change: 114795 $  $DateTime: 2012/09/10 13:52:48 $  $Author: Arik.Sher $
 */

import java.io.Serializable;

import org.springframework.util.Assert;

import com.ebs.commons.comp.IParsedPayload;
import com.ebs.md.api.deal.IDeal;
import com.ebs.md.api.deal.IDealAdapter;
import com.ebs.md.api.hit.IHitSubmit;
import com.ebs.md.api.hit.IHitSubmitAdapter;
import com.ebs.md.fxmonitor.marketmonitor.AbstractMonitorListenerAndForwarder;


public class ParsedPayloadToObjAdapter
extends AbstractMonitorListenerAndForwarder<Serializable>
{
    final static String MQ_MSG_NAME_HIT_SUBMIT = "RHST"; // HITSUBMIT
    final static String MQ_MSG_NAME_DEAL       = "RDSM"; // DEALSTATUSMAKER - looking only at Local (region) Makers(NA_DSM)

	protected IDealAdapter<IParsedPayload, IDeal>           dealAdapter;
	protected IHitSubmitAdapter<IParsedPayload, IHitSubmit> hitSubmitAdapter;
	
	public void setDealAdapter(IDealAdapter<IParsedPayload, IDeal> dealAdapter) {
		this.dealAdapter = dealAdapter;
	}
	public void setHitSubmitAdapter(IHitSubmitAdapter<IParsedPayload, IHitSubmit> hitSubmitAdapter) {
		this.hitSubmitAdapter = hitSubmitAdapter;
	}
	
	public void init() {
		super.init();
		Assert.notNull(dealAdapter,      "'dealAdapter' cannot be null");
		Assert.notNull(hitSubmitAdapter, "'hitSubmitAdapter' cannot be null");
	}

	@Override
	protected Serializable transform(IParsedPayload payload) {
        String mqMessageType  = payload.getParsedStringField("mq_msg_type");
        
        Serializable forwardObject = null;
        
        if ( MQ_MSG_NAME_HIT_SUBMIT.equals(mqMessageType) ) {
        	IHitSubmit converted = hitSubmitAdapter.convert(payload);
        	forwardObject = converted;
        }
        if ( MQ_MSG_NAME_DEAL.equals(mqMessageType) ) {
        	IDeal converted = dealAdapter.convert(payload);
        	forwardObject = converted;
        }
        
        return forwardObject;
	}
	
}
